/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.controller;

import com.minierp.bean.MessageBean;
import com.minierp.bean.UserBean;
import com.minierp.dao.AccessoryDao;
import com.minierp.dao.IdGenerator;
import com.minierp.dao.MessageDao;
import com.minierp.helper.EncCommonHelper;
import com.minierp.model.Message;
import com.minierp.model.MessageReceiver;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

public class MessageFormCtr extends SimpleFormController {
	private MessageDao messageDao;
	private AccessoryDao accessoryDao;
	private IdGenerator idGenerator;

	protected Map referenceData(HttpServletRequest request) throws Exception {
		HashMap refData = new HashMap();
		String serverName = request.getServerName();
		String serverPort = String.valueOf(EncCommonHelper.getEnterpriseInfo(
				request).getUploadPort());
		refData.put("serverName", serverName);
		refData.put("serverPort", serverPort);
		return refData;
	}

	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {
		String replyId = request.getParameter("reply_id");
		MessageBean bean = new MessageBean();
		Message form = new Message();
		String tmpCode;
		if (replyId != null && !replyId.equals("")) {
			Message ub1 = (Message) this.messageDao.get(Message.class,
					Long.parseLong(replyId));
			if (ub1 != null) {
				bean.setReceiverIdStr(String.valueOf(ub1.getSenderId()));
				bean.setReceiverNameStr(ub1.getSenderName());
				form.setTitle("Re:" + ub1.getTitle());
				form.setContent(ub1.getContent());
			}
		} else {
			String ub = request.getParameter("receiver_id");
			tmpCode = request.getParameter("receiver_name");
			String title = request.getParameter("title");
			String content = request.getParameter("content");
			String relationId = request.getParameter("relation_id");
			bean.setReceiverIdStr(ub);
			bean.setReceiverNameStr(tmpCode);
			form.setTitle(title);
			form.setContent(content);
			if (relationId != null && !relationId.equals("")) {
				form.setRelationId(Long.parseLong(relationId));
			}
		}

		bean.setMessage(form);
		UserBean ub2 = EncCommonHelper.getCurrentUserAccount(request);
		tmpCode = ub2.getUser().getId() + "_" + System.currentTimeMillis();
		bean.setTmpCode(tmpCode);
		return bean;
	}

	protected void onBind(HttpServletRequest request, Object command)
			throws Exception {
		MessageBean form = (MessageBean) command;
		form.setAccessories(this.accessoryDao.getList("MESSAGE",
				form.getTmpCode(), (String[]) null, 0L));
	}

	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		ModelAndView mv = new ModelAndView(this.getSuccessView());
		UserBean ub = EncCommonHelper.getCurrentUserAccount(request);
		MessageBean bean = (MessageBean) command;
		Message obj = bean.getMessage();
		String[] idArr = bean.getReceiverIdStr().split(";");
		String[] nameArr = bean.getReceiverNameStr().split(";");
		ArrayList sendedArr = new ArrayList();
		ArrayList arr = new ArrayList();

		for (int receiverName = 0; receiverName < idArr.length; ++receiverName) {
			if (!sendedArr.contains(idArr[receiverName])) {
				MessageReceiver stafferName = new MessageReceiver();
				stafferName.setReceiverId(Long.parseLong(idArr[receiverName]));
				stafferName.setReceiverName(nameArr[receiverName]);
				arr.add(stafferName);
				sendedArr.add(idArr[receiverName]);
			}
		}

		String arg20 = "";
		if (arr.size() > 0) {
			arg20 = ((MessageReceiver) arr.get(0)).getReceiverName();
			if (arr.size() > 1) {
				arg20 = arg20 + "...";
			}
		}

		obj.setId(this.idGenerator.getId(Message.class.getName()));
		String arg21 = "";
		long stafferId = 0L;
		if (ub.getStaffer() != null) {
			arg21 = ub.getStaffer().getStafferName();
			stafferId = ub.getStaffer().getId();
		}

		obj.setSenderId(stafferId);
		obj.setSenderName(arg21);
		obj.setSendTime(new Date());
		obj.setReceiverName(arg20);
		if (this.accessoryDao.getList("MESSAGE", bean.getTmpCode(),
				(String[]) null, 0L).size() > 0) {
			obj.setHasAttach("Y");
		} else {
			obj.setHasAttach("N");
		}

		this.messageDao.save(obj);
		this.accessoryDao.update(bean.getTmpCode(), obj.getId());
		long curId = this.idGenerator.getId(MessageReceiver.class.getName(),
				arr.size());

		for (int i = 0; i < arr.size(); ++i) {
			MessageReceiver r = (MessageReceiver) arr.get(i);
			r.setId(curId);
			r.setMessageId(obj.getId());
			this.messageDao.save(r);
			++curId;
		}

		EncCommonHelper.setAlertMessage(request, "发送成功");
		return mv;
	}

	public IdGenerator getIdGenerator() {
		return this.idGenerator;
	}

	public void setIdGenerator(IdGenerator idGenerator) {
		this.idGenerator = idGenerator;
	}

	public MessageDao getMessageDao() {
		return this.messageDao;
	}

	public void setMessageDao(MessageDao messageDao) {
		this.messageDao = messageDao;
	}

	public AccessoryDao getAccessoryDao() {
		return this.accessoryDao;
	}

	public void setAccessoryDao(AccessoryDao accessoryDao) {
		this.accessoryDao = accessoryDao;
	}
}